﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
namespace ControlAccessUser.DataAccess
{
    public static class DataAccessUser
    {
        public static ICollection<Model.User> SearchUserByUserNameAndUserPassword(Model.User UserToVerify)
        {
            return (from user in ControlAccessUserEntities.Instance.Users
                    where
                    user.UserName == UserToVerify.UserName &&
                    user.UserPassword == UserToVerify.UserPassword
                    select user).ToList();
        }

        public static void InsertNewUser(Model.User NewUser)
        {
            DataAccess.ControlAccessUserEntities.Instance.Users.Add(NewUser);
        }

        public static ICollection<Model.User> SearhcUserByUserName(Model.User UserForSearch)
        {
            return (from User in ControlAccessUserEntities.Instance.Users
                    where User.UserName.ToUpper()==UserForSearch.UserName.ToUpper()
                    select User
                    ).ToList();
        }

        public static ICollection<Model.User> SearchUserByUserId(Model.User UserForSearch)
        {
            return (from User in ControlAccessUserEntities.Instance.Users
                    where User.UserId==UserForSearch.UserId
                    select User
                    ).ToList();
        }

        public static ICollection<Model.User> SearchUserByRelativeFields(Model.User UserForSearch)
        {
            string UserId=""; 
            if(UserForSearch.UserId>=0)
                UserId = UserForSearch.UserId.ToString(System.Globalization.CultureInfo.CurrentCulture);
            
            return (from User in ControlAccessUserEntities.Instance.Users
                    where User.UserFirstName.Contains(UserForSearch.UserFirstName)
                        && User.UserLastName.Contains(UserForSearch.UserLastName)
                        && User.UserName.Contains(UserForSearch.UserName)
                        && System.Data.Objects.SqlClient.SqlFunctions.StringConvert((double)User.UserId).Contains(UserId)
                    select User
                    ).ToList();
        }
    }
}
